package com.reflect;

import java.lang.reflect.Field;

import com.entity.UserEntity;
/****
 * 反射测试
 * @author Administrator
 *
 */
public class ReflectTest {

	
	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException {
		// 获取当前类class地址
				Class<?> forName = Class.forName("com.entity.UserEntity");
				// 使用反射实例化对象 无参数构造函数
				Object newInstance = forName.newInstance();
				// 获取当前类的 userId字段
				Field declaredField = forName.getDeclaredField("userId");
				// 允许操作私有成员
				declaredField.setAccessible(true);
				// 设置值
				declaredField.set(newInstance, "123");
				UserEntity user = (UserEntity) newInstance;
				System.out.println(user.getUserId());
	}
}
